<!--
@Description:数据字典多选框选择
@Author:zqy
@Date:2022-08-19 11:12:29
@Last Modified by:zqy
@Last Modified time:2022-08-19 11:12:29
 -->
<template>
  <el-checkbox-group v-model="curValue" v-bind="$attrs" v-on="$listeners">
    <slot name="before"></slot>
    <el-checkbox v-for="(item,i) in comOptList" :key="i" :label="item.dictCode">{{ item.dictName }}</el-checkbox>
    <slot></slot>
  </el-checkbox-group>
</template>
<script>
import { getDictByType } from '../DictSelectPicker/apis'
export default {
  name: 'QupDictCheckbox',
  inheritAttrs: false,
  props: {
    dictTypeCode: {
      // 数据字典类型编码
      type: String,
      default: ''
    },
    // 业务域编码
    domainCode:{
      type:String,
      default:'',
      required:true
    },
    // 应用编码
    appCode:{
      type:String,
      default:'',
      required:true
    },
    value: {
      // 选中值
      type: [Array],
      default: () => []
    },
    includes: {
      // 包含指定的字典编码,若为Function则返回值需要是字典编码数组
      type: [Array,Function],
      default: undefined
    },
    excludes: {
      // 排除指定的字典编码,若为Function则返回值需要是字典编码数组
      type: [Array,Function],
      default: undefined
    }
  },
  data() {
    return {
      optList: [],
      curValue: [],
      exCodeList:[],
      inCodeList:[]
    }
  },
  computed: {
    comOptList() {
      let t = this.optList
      if (this.inCodeList instanceof Array && this.inCodeList.length>0) {
        t = t.filter(e => this.inCodeList.some(v=>v.toString()===e.dictCode.toString()))
      }
      if (this.exCodeList instanceof Array && this.exCodeList.length > 0) {
        t = t.filter(e =>!this.exCodeList.some(v=>v.toString()===e.dictCode.toString()))
      }
      return t
    }
  },
  watch: {
    value: {
      handler(n) {
        this.curValue = n
      },
      immediate: true
    },
    curValue(n) {
      this.$emit('input', n)
    },
    dictTypeCode: {
      handler(nv, ov) {
        this.getDataDict()
      }
    },
  },
  created() {
    this.getDataDict()
  },
  methods: {
    getDataDict() {
      let params = {}
      if (this.dictTypeCode && this.domainCode && this.appCode) {
        params = { dictTypeCode: this.dictTypeCode, appCode: this.appCode, domainCode: this.domainCode }
      } else {
        this.optList = []
        return
      }
      getDictByType(params).then(res => {
        this.optList = res.data || []
        this.getCludeList()
        this.$emit('data-change',this.optList)
      })
    },
    getCludeList(){
      let inCodeList = []
      if(this.includes){
        if(this.includes instanceof Array){
            inCodeList = this.includes
        }else if(this.includes instanceof Function){
          try {
            const result = this.includes()
            if (result instanceof Promise) {
              result.then((data) => {
                inCodeList = data instanceof Array ? data : []
              })
            } else {
              inCodeList = result instanceof Array ? result : []
            }
          } catch (e) {
            this.inCodeList = []
          }
        }
      }
      this.inCodeList = inCodeList

      let exCodeList = []
      if(this.excludes){
        if(this.excludes instanceof Array){
            exCodeList = this.excludes || []
        }else if(this.excludes instanceof Function){
          try {
            const result = this.excludes()
            if (result instanceof Promise) {
              result.then((data) => {
                exCodeList = data instanceof Array ? data : []
              })
            } else {
              exCodeList = result instanceof Array ? result : []
            }
          } catch (e) {
            exCodeList = []
          }
        }
      }
      this.exCodeList = exCodeList
    },
  }
}
</script>
<style lang="scss" scoped>

</style>
